package com.example.lianxi1;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.lianxi1.adapter.ListFragment;
import com.google.android.material.tabs.TabLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    private TabLayout mTablayout;
    private ViewPager mVp;
    ArrayList<Fragment> list=new ArrayList<>();
    ArrayList<String> title=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
       new Retrofit.Builder()
               .baseUrl(APiService.url)
               .addConverterFactory(GsonConverterFactory.create())
               .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
               .build().create(APiService.class)
               .getdatas().subscribeOn(Schedulers.io())
               .observeOn(AndroidSchedulers.mainThread())
               .subscribe(new Observer<Bean>() {
                   @Override
                   public void onSubscribe(Disposable d) {

                   }

                   @Override
                   public void onNext(Bean bean) {
                       List<Bean.DataBean> data = bean.getData();
                       for (int i = 0; i < data.size(); i++) {
                           String name = data.get(i).getName();
                           mTablayout.addTab(mTablayout.newTab().setText(name));
                           ListFragment listFragment = new ListFragment();
                           list.add(listFragment);
                           title.add(name);

                           List<AlbumsBean> albums = data.get(i).getAlbums();
                           Bundle bundle = new Bundle();
                           bundle.putSerializable("list",(Serializable) albums);

                           listFragment.setArguments(bundle);

                       }
                       initVT();
                       mVp.setCurrentItem(2);
                       mTablayout.setupWithViewPager(mVp);
                   }


                   @Override
                   public void onError(Throwable e) {

                   }

                   @Override
                   public void onComplete() {

                   }
               });

    }

    private void initVT() {
        mVp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return list.get(position);
            }

            @Override
            public int getCount() {
                return list.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return title.get(position);
            }
        });
    }
    private void initView() {
        mTablayout = (TabLayout) findViewById(R.id.tablayout);
        mVp = (ViewPager) findViewById(R.id.vp);
    }
}
